package com.njupt.yanglonglong.report.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njupt.yanglonglong.report.demo.entity.Course;
import com.njupt.yanglonglong.report.demo.entity.CourseExperiment;
import com.njupt.yanglonglong.report.demo.entity.RCourseTeacher;
import com.njupt.yanglonglong.report.demo.mapper.CourseMapper;
import com.njupt.yanglonglong.report.demo.mapper.RCourseTeacherMapper;
import com.njupt.yanglonglong.report.demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author yangllong
 * @Date 2021/4/25 17:55
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private RCourseTeacherMapper rCourseTeacherMapper;

    @Override
    public List<Course> findAll(String courseName) {
        QueryWrapper qw = new QueryWrapper();
        qw.like("course_name", courseName);
        return courseMapper.selectList(qw);
    }

    @Override
    public List<Course> getCoursesByTeacherId(String teacherId) {
        return courseMapper.getCoursesByTeacherId(teacherId);
    }

    @Override
    public List<CourseExperiment> getExperimentByTeacherId(String teacherId) {
        return courseMapper.getExperimentByTeacherId(teacherId);
    }

    @Override
    public int addCourse(Course course) {
        return courseMapper.insert(course);
    }

    @Override
    public boolean deleteCourse(Course course) {
        QueryWrapper wq = new QueryWrapper();
        wq.eq("course_id", course.getCourseId());
        RCourseTeacher rCourseTeacher = rCourseTeacherMapper.selectOne(wq);
        if (rCourseTeacher == null) {
            courseMapper.deleteById(course.getId());
            return true;
        }
        return false;
    }

    @Override
    public void editCourse(Course course) {
        courseMapper.updateById(course);
    }

    @Override
    public List<Course> queryStudentCourseList(String stuId) {
        return courseMapper.getStuCourseList(stuId);
    }

}
